# -*- coding:utf-8 _*-  
""" 
@author:yanming
@file: main.py 
@version:
@time: 2020/07/26 
@email:476588294@qq.com
@function： 
"""
# 系统自带包
import os
import sys
import time
import traceback
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QSplashScreen, qApp
from PyQt5.QtCore import QDir, QProcess
from src.win_interface import HomePage
import src.resouce_rc


def wait_loading(times, sp, error):
    for i in range(times):
        path = os.path.join(os.getcwd(), "error.log")
        if error:
            info = '<font color=#FA4C35 size=5> %s </font>' \
                   % """<b>启动错误({}s):{}. 详见{} </b>""".format(times - i, error, path)
            sp.showMessage(info, Qt.AlignCenter, Qt.black)
        else:
            info = '<font color=#FFFFFF size=4> %s </font>' \
                   % """<b>系统加载中，请稍等...</b>""".format(times - i)
            sp.showMessage(info, Qt.AlignHCenter | Qt.AlignBottom, Qt.black)
        time.sleep(1)
        qApp.processEvents()


def main():
    app = QApplication(sys.argv)
    splash = QSplashScreen(QPixmap(r':/icon/icon/loading.png'))
    splash.show()
    wait_loading(3, splash, None)

    try:
        home = HomePage()
        splash.finish(home)
        home.show()
        exec_code = app.exec_()

        if exec_code == 8888:
            # program = QApplication.applicationFilePath()
            program = r'D:\WorkSpace\PDMs\PDMs\venv\Scripts\python.exe'
            args = QApplication.arguments()
            work_dir = QDir.currentPath()
            QApplication.exit()
            QProcess.startDetached(program, args, work_dir)
        else:
            sys.exit()
    except SystemExit:
        pass

    except Exception as error:
        wait_loading(10, splash, error)
        traceback.print_exc(file=open('error.log', 'w+'))
        QApplication.exit()


if __name__ == '__main__':
    main()
